<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use App\Models\BClass;
use App\Models\BGoods;
use App\Models\Classify;
use App\Models\Goods;
use App\Models\Order;
use App\Models\Pictures;
use App\Models\Remit;
use App\Models\Shop;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CountController extends Controller
{
    //订单统计
    public function order(Request $request)
    {
        if($request->has(['star','end'])){
            $order = Order::whereIn('order_status',[2,3,4,5])
                ->whereBetween('created_at',[$request->get('star'),$request->get('end')])
                ->paginate(100);
        }else{
            $order = Order::whereIn('order_status',[2,3,4,5])->orderBy('created_at','desc')->paginate(15);
        }
        $goods_sum = Order::whereIn('order_status',[3,4,5])->sum('sum_price');
        $count = Order::whereIn('order_status',[3,4,5])->count();
        $receive_type1 = Order::whereIn('order_status',[3,4,5])->where('receive_type',1)->count();
        $receive_type2 = Order::whereIn('order_status',[3,4,5])->where('receive_type',2)->count();

        return view('admin.count_order',compact(['order','goods_sum','count','receive_type1','receive_type2']));
    }

    public function goods(Request $request)
    {
        if($request->isMethod('post')){
            if($request->has('goods_id')){
                Goods::where('goods_id',$request->get('goods_id'))->increment('stock',$request->get('stock'));
                return redirect()->back()->with(['message'=>"添加库存成功"]);
            }
        }
        $goods = Goods::where('status',1)->orderBy('created_at','desc')->paginate(10);
        if($request->has('keywords')){
            $goods = Goods::where('status',1)
                ->where('name','like','%'.$request->get('keywords').'%')
                ->paginate(100);
        }
        if($request->has('classify')){
            if(!$request->get('classify')==0){
                $class = Classify::find($request->get('classify'));
                if($class->level==1){
                    $class2 = Classify::where('pa_id',$class->classify_id)->get()->toArray();
                    foreach ($class2 as $val){
                        $where[] = $val['classify_id'];
                    }
                    $goods = Goods::where('status',1)->whereIn('class_id',$where)->orderBy('created_at','desc')->paginate(100);
                }else{
                    $goods = Goods::where('status',1)->where('class_id',$class->classify_id)->orderBy('created_at','desc')->paginate(100);
                }
            }
        }

        if($request->has('sell')){
            $goods = Goods::where('status',1)->orderBy('sell',$request->get('sell'))->paginate(100);
        }

        if($request->has('stock')){
            $goods = Goods::where('status',1)->orderBy('stock',$request->get('stock'))->paginate(100);
        }

        foreach ($goods as $value){
            $img = $value->my_picture()->where('type',1)->first();
            $cover = Pictures::find($img->pic_id)->toArray();
            $value->my_cover = $cover['pic_path'];
        };
        $goods_sum=Goods::count();
        $up_goods = Goods::where('status',1)->count();
        $sell = Goods::sum('sell');
        $stock = Goods::sum('stock');
        $classify = Classify::where('level',1)->get()->toArray();
        $arr=[];
        foreach ($classify as $value){
            $classify2 = Classify::where('pa_id',$value['classify_id'])->get()->toArray();
            $arr[]=$value;
            foreach ($classify2 as $v){
                $arr[]=$v;
            }
        }
        $selected=$request->get('classify',0);
        return view('admin.count_goods',compact(['goods_sum','up_goods','goods','sell','stock','arr','selected']));
    }

    //统计
    public function b_order()
    {
        $admin = Admin::where('type',2)->get();
        $shop = Shop::all();
        $class = BClass::all();
        $user = Session::get('admin');
        if($user->type==1){
            $res = json_decode($this->https_request('http://egui.gebixianxiang.com:8081/BGoods/Data//byall?pages=1'),true);
        }else{
            $res = json_decode($this->https_request(' http://egui.gebixianxiang.com:8081/BGoods/Data/bydistributor?pages=1'),true);
        };
        return view('admin.b_order',['admin'=>$admin,'shop'=>$shop,'user'=>$user,'class'=>$class,'pages'=>(int)ceil($res['Data']['num']/15)]);
    }

    public function b_goods(Request $request)
    {
        if($request->isMethod('post')){
            $goods = BGoods::find($request->get('goods_id'));
            if($request->has('price')){
                $goods->price=$request->get('price');
            }else{
                $goods->status=3;
            }
            $goods->save();
            return redirect()->back()->with(['message'=>"修改成功"]);
        }
        $admin = Admin::where('type',2)->get();
        $shop = Shop::all();
        $class = BClass::all();
        $user = Session::get('admin');
        $res = json_decode($this->https_request('http://egui.gebixianxiang.com:8081/BGoods/Data/BgoodsbyAll?pages=1'),true);
        return view('admin.b_goods',['admin'=>$admin,'shop'=>$shop,'class'=>$class,'user'=>$user,'pages'=>(int)ceil($res['unm']/15)]);
    }

    public function ask()
    {
        return view('admin.ask');
    }


    public function ask2(Request $request)
    {
        $id=$request->get('id');

        return view('admin.ask2',['id'=>$id]);
    }

    public function ask3(Request $request)
    {
        $id=$request->get('id');

        return view('admin.ask3',['id'=>$id]);
    }

    public function ask4(Request $request)
    {
        $id=$request->get('id');

        return view('admin.ask4',['id'=>$id]);
    }

    public function ask5(Request $request)
    {
        $id=$request->get('id');

        return view('admin.ask5',['id'=>$id]);
    }

    public function this_order(Request $request)
    {
        $type=$request->get('type');
        $id=$request->get('id');
        return view('admin.this_order',['type'=>$type,'id'=>$id]);
    }

    public function this_all(Request $request)
    {
        $type=$request->get('type');
        return view('admin.this_all',['type'=>$type]);
    }


    public function all_son(Request $request)
    {
        $type=$request->get('type');
        return view('all_order',['type'=>$type]);
    }
}
